# -*- coding: utf-8 -*-
# @Time    : 2023/10/31 14:39
# @Author  : floraachy
# @File    : test_repo_issue_flow.py
# @Software: PyCharm
# @Desc:

# 标准库导入
import os
# 第三方库导入
import allure
# 本地应用/模块导入
from utils.ui_utils.base_page import BasePage
from utils.report_utils.allure_handle import allure_step


class RepoIssuePage(BasePage):

    @allure.step("点击【创建疑修】按钮")
    def click_create_issue_button(self):
        """
        点击'创建疑修'按钮
        """
        self.click(locator="//a[text()='创建疑修']")
        self.page.wait_for_load_state()

    @allure.step("输入疑修标题: {issue_title}")
    def input_issue_title(self, issue_title: str):
        """
        输入疑修标题
        """
        self.page.wait_for_timeout(300)  # 超时时间是0.3秒
        self.input(locator="id=subject", text=issue_title)

    @allure.step("输入疑修内容: {issue_desc}")
    def input_issue_desc(self, issue_desc: str):
        """
        输入疑修内容
        """
        # todo 疑惑：这里必须加wait_for_timeout，否则即使用的wait_for_selector，也无法输入内容。
        self.page.wait_for_timeout(1000)
        # elem = page.wait_for_selector(
        #     selector="//textarea/following::div[contains(@class, 'CodeMirror-wrap')]", timeout=10000, state='visible')
        elem = self.page.query_selector(selector="//textarea/following::div[contains(@class, 'CodeMirror-wrap')]")
        self.execute_js('''([elem, content]) => elem.CodeMirror.setValue(content);''',
                        [elem, issue_desc])

    @allure.step("上传疑修附件: {issue_file}")
    def upload_issue_attachments(self, issue_file: str):
        """
        上传疑修附件
        """
        self.upload_file(locator="//p[text()='拖动文件或点击此处上传']/parent::div/preceding-sibling::input",
                         file_path=issue_file)

    @allure.step("点击【编辑】按钮，进入编辑疑修页面")
    def click_edit_issue_button(self):
        """
        点击'编辑'按钮，进入编辑疑修页面
        """
        self.click(locator="//a[contains(text(), '编辑')]")

    @allure.step("点击【复制】按钮，进入复制疑修页面")
    def click_copy_issue_button(self):
        """
        点击'复制'按钮，进入复制疑修页面
        """
        self.click(locator="//a[contains(text(), '复制')]")

    @allure.step("点击文件：{file_name} 对应的删除图标")
    def click_delete_attach_icon(self, file_name):
        """
        点击疑修删除附件图标
        """
        # 根据文件名称寻找对应文件的删除图标
        self.click(f"//span[contains(text(),'{file_name}')]/parent::a/following-sibling::span/i")

    @allure.step("点击【创建】按钮，提交表单校验，进行疑修创建")
    def submit_issue(self):
        """
        点击'创建'按钮，提交表单校验，进行疑修创建
        """
        self.click(locator="//span[text()='创 建']/parent::button")
        self.page.wait_for_timeout(3000)

    @allure.step("点击【保存】按钮，提交表单校验，进行疑修更新")
    def save_issue(self):
        """
        点击'保存'按钮，提交表单校验，进行疑修更新
        """
        self.click("//span[contains(text(), '保 存')]/..")
        self.page.wait_for_timeout(3000)

    @allure.step("疑修详情，点击【删除】按钮，进行删除疑修操作")
    def click_delete_issue_button(self):
        """
        疑修详情，点击'删除'按钮
        """
        self.click("//a[text()='删除']")
        self.page.wait_for_timeout(3000)

    @allure.step("在搜索框中，输入疑修关键字: {text}，点击搜索图标，搜索疑修")
    def search_issue_by_keyword(self, text):
        """
        在搜索框中，输入疑修关键字，点击搜索图标，搜索疑修
        """
        self.input(locator="//input[@placeholder='输入关键字搜索疑修']", text=text)
        self.click("//i[contains(@class, 'search')]")
        self.page.wait_for_timeout(3000)

    @allure.step("在疑修列表点击疑修标题：{title}")
    def click_issue_title_on_list(self, title):
        """
        在疑修列表点击疑修标题
        """
        self.click(f"//div[@class= 'listdatas']//a[contains(text(), '{title}')]")
        self.page.wait_for_timeout(3000)

    @allure.step("获取疑修所有附件")
    def get_issue_attachments(self):
        """
        获取疑修所有附件
        """
        return self.get_all_elements_text(
            locator="//div[contains(@class, 'attachment-list-div')]//span[1]")

    @allure.step("断言 --> 删除疑修二次确认弹窗弹出， 预期结果：{text}")
    def assert_delete_issue_confirm_pop_show(self, text):
        """
        删除疑修二次确认弹窗弹出
        """
        self.is_element_visible(locator=f"//p[text()='{text}']")

    @allure.step("断言 --> 易修标题, 预期结果：{title}")
    def assert_issue_title(self, title: str):
        """
        检查易修标题
        """
        self.contain_text(locator="//div[@class='detailtitle']/div/p", text=title)

    @allure.step("断言 --> 易修内容, 预期结果：{desc}")
    def assert_issue_desc(self, desc: str):
        """
        检查易修内容
        """
        self.contain_text(locator="//div[@class='descPanel']/div/p", text=desc)

    @allure.step("断言 --> 易修附件, 预期结果：{files}")
    def assert_issue_attachments(self, files):
        """
        检查易修附件
        """
        actual_issue_files = self.get_all_elements_text(
            locator="//div[contains(@class, 'attachment-list-div')]//span[@class='ml20'][1]/preceding-sibling::span")
        expected_issue_files = []
        for file_path in files:
            if os.path.isabs(file_path):
                expected_issue_files.append(os.path.basename(file_path))
            else:
                expected_issue_files.append(file_path)
        allure_step(step_title=f"预期结果：{expected_issue_files} = 实际结果：{actual_issue_files}")
        assert expected_issue_files == actual_issue_files

    def new_issue(self, issue_title, issue_desc, issue_file):
        """
        新建疑修:
        1. 输入疑修标题
        2. 输入疑修内容
        3. 上传疑修附件
        4. 点击"创建"按钮
        """
        self.input_issue_title(issue_title)
        self.input_issue_desc(issue_desc)
        self.upload_issue_attachments(issue_file)
        self.submit_issue()
